package com.wpd.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import com.wpd.bo.WpdOrder;
import com.wpd.bo.WpdUser;
import com.wpd.service.IWpdOrderService;
import com.wpd.dao.IWpdOrderDao;
import com.common.bo.PageBean;
import com.common.util.StringUtil;

/**
 *
 *  @author : 自动生成
 *  @date   : 2017年07月14日 10:18:16
 *  @desc   : WpdOrder服务层实现类定义
 */

@Service
@Transactional
public class WpdOrderServiceImpl implements IWpdOrderService  {
	
	@Autowired
	private IWpdOrderDao wpdOrderDao;
	
	@Override
    public Integer insert(WpdOrder wpdOrder, WpdUser user){
		wpdOrder.setApplyTime(new Date());
		wpdOrder.setApplicantGuid(user.getGuid());
		wpdOrder.setApplicantName(user.getUserName());
    	return wpdOrderDao.insert(wpdOrder);
    }
    @Override
    public void insertBatchEntity(List<WpdOrder> wpdOrderList){
    	wpdOrderDao.insertBatchEntity(wpdOrderList);
    }
    @Override
    public Integer delete(WpdOrder wpdOrder){
    	return wpdOrderDao.delete(wpdOrder);
    }
    @Override
    public Integer update(WpdOrder wpdOrder, WpdUser user){
    	WpdOrder oldOrder = wpdOrderDao.getBeanById(wpdOrder.getGuid());
    	if(!StringUtil.isEmpty(wpdOrder.getApprovalFlag())){
    		if(!wpdOrder.getApprovalFlag().equals(oldOrder.getApprovalFlag())){
    			wpdOrder.setApplyTime(new Date());
    			wpdOrder.setApproverGuid(user.getGuid());
    			wpdOrder.setApproverName(user.getUserName());
    		}
    	}
    	if(!StringUtil.isEmpty(wpdOrder.getSendFlag())){
    		if(!wpdOrder.getSendFlag().equals(oldOrder.getSendFlag())){
    			wpdOrder.setSendTime(new Date());
    			wpdOrder.setSenderGuid(user.getGuid());
    			wpdOrder.setSenderName(user.getUserName());
    		}
    	}
    	if(!StringUtil.isEmpty(wpdOrder.getExecuteFlag())){
    		if(!wpdOrder.getExecuteFlag().equals(oldOrder.getExecuteFlag())){
    			if(StringUtil.isEmpty(oldOrder.getExecuteStartTime())){
    				wpdOrder.setExecuteStartTime(new Date());
    			}else{
    				wpdOrder.setExecuteEndTime(new Date());
    			}
    		}
    	}
    	if(!StringUtil.isEmpty(wpdOrder.getExceptRepairTimeStr())){
    		wpdOrder.setExceptRepairTime(new Date(wpdOrder.getExceptRepairTimeStr()));
    	}
    	return wpdOrderDao.update(wpdOrder);
    }
    @Override
    public List<WpdOrder> query(WpdOrder wpdOrder){
    	return wpdOrderDao.query(wpdOrder);
    }
    @Override
    public int getTotalCount(WpdOrder wpdOrder){
    	return wpdOrderDao.getTotalCount(wpdOrder);
    }
    @Override
    public List<WpdOrder> queryPageList(WpdOrder wpdOrder, PageBean pageBean){
		return wpdOrderDao.queryPageList(wpdOrder, pageBean.getStartPageIndex(), pageBean.getPageSize());
	}
	@Override
	public WpdOrder getBean(WpdOrder wpdOrder){
		return wpdOrderDao.getBeanById(wpdOrder.getGuid());
	}
}
